/*
 * Copyright (C) 2012 Trilarion
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.anote;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.WindowConstants;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import org.tools.ui.helper.LookAndFeel;

/**
 *
 * @author Trilarion 2012
 */
public class HelpBrowserExample extends JFrame {

    private static final Logger LOG = Logger.getLogger(HelpBrowserExample.class.getName());

    private static final long serialVersionUID = 1L;

    /**
     * Creates new form HelpBrowserExample
     */
    public HelpBrowserExample() {
        initComponents();

        URL url = HelpBrowserExample.class.getClass().getResource("/html/index.html");
        try {
            editorPane.setPage(url);
        } catch (IOException ex) {
            LOG.log(Level.SEVERE, null, ex);
        }

        editorPane.addHyperlinkListener(new HyperlinkListener() {

            @Override
            public void hyperlinkUpdate(HyperlinkEvent e) {
                if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                    try {
                        editorPane.setPage(e.getURL());
                    } catch (IOException ex) {
                        LOG.log(Level.SEVERE, null, ex);
                    }
                }
            }
        });

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        backButton = new JButton();
        scrollPane = new JScrollPane();
        editorPane = new JEditorPane();
        forwardButton = new JButton();
        indexButton = new JButton();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setTitle("HelpBrowser Test");

        backButton.setText("<<");
        backButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });

        editorPane.setContentType("text/html");
        editorPane.setEditable(false);
        scrollPane.setViewportView(editorPane);

        forwardButton.setText(">>");
        forwardButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                forwardButtonActionPerformed(evt);
            }
        });

        indexButton.setText("Index");
        indexButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                indexButtonActionPerformed(evt);
            }
        });

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(backButton)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(indexButton)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(forwardButton)
                .addContainerGap(380, Short.MAX_VALUE))
            .addComponent(scrollPane)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(backButton)
                    .addComponent(forwardButton)
                    .addComponent(indexButton))
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void backButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_backButtonActionPerformed

    private void indexButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_indexButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_indexButtonActionPerformed

    private void forwardButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_forwardButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_forwardButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        LookAndFeel.setSystemLookAndFeel();

        /*
         * Create and display the form
         */
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new HelpBrowserExample().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private JButton backButton;
    private JEditorPane editorPane;
    private JButton forwardButton;
    private JButton indexButton;
    private JScrollPane scrollPane;
    // End of variables declaration//GEN-END:variables
}
